/*
 *    Copyright (C) 2011-2012 Hai Bison
 *
 *    See the file LICENSE at the root directory of this project for copying
 *    permission.
 */

package group.pals.mailgate.ui;

import group.pals.mailgate.beans.ItemEmailMessage;
import group.pals.mailgate.threads.EmailReader;
import group.pals.mailgate.preferences.Preferences;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import org.apache.commons.lang3.StringEscapeUtils;

/**
 * Most of code in this class is based on this tips:<br>
 * <a href="http://www.java2s.com/Code/Java/Swing-JFC/TreeCellRenderer.htm">
 * http://www.java2s.com/Code/Java/Swing-JFC/TreeCellRenderer.htm</a>
 * @author Hai Bison
 */
public class EmailMessageCellRenderer extends DefaultTreeCellRenderer {

    private final PanelEmailMessage _PanelEmailMessage;
    private int panelEmailMessagePaddingWidth;

    public EmailMessageCellRenderer() {
        this._PanelEmailMessage = new PanelEmailMessage();
        this.panelEmailMessagePaddingWidth = Preferences.DefPopupWindowPaddingWidth;
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        if (value instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            Object userObject = node.getUserObject();
            if (userObject instanceof EmailReader) {
                setForeground(Color.darkGray);
                setFont(getFont().deriveFont(Font.BOLD));
            } else if (userObject instanceof ItemEmailMessage) {
                ItemEmailMessage emailMessage = (ItemEmailMessage) userObject;
                EmailReader emailReader = null;
                if (node.getParent() instanceof DefaultMutableTreeNode
                        && ((DefaultMutableTreeNode) node.getParent()).getUserObject() instanceof EmailReader) {
                    emailReader = (EmailReader) ((DefaultMutableTreeNode) node.getParent()).getUserObject();
                }
                _PanelEmailMessage.setEmailMessageInfo(emailMessage, emailReader,
                        tree.getParent() != null ? tree.getParent() : tree,
                        panelEmailMessagePaddingWidth);
                _PanelEmailMessage.setBackground(backgroundNonSelectionColor);
                _PanelEmailMessage.setSelected(selected);
                _PanelEmailMessage.setEnabled(tree.isEnabled());

                _PanelEmailMessage.setToolTipText(String.format(
                        "<html><b>Sender:</b> %s", StringEscapeUtils.escapeHtml4(emailMessage.getSender())));
                return _PanelEmailMessage;
            }
        }

        return super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
    }

    /**
     * @return the panelEmailMessagePaddingWidth
     */
    public int getPanelEmailMessagePaddingWidth() {
        return panelEmailMessagePaddingWidth;
    }

    /**
     * @param panelEmailMessagePaddingWidth the panelEmailMessagePaddingWidth to set
     */
    public void setPanelEmailMessagePaddingWidth(int panelEmailMessagePaddingWidth) {
        this.panelEmailMessagePaddingWidth = panelEmailMessagePaddingWidth;
    }
}
